package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;
import com.thoughtworks.xstream.XStream;


public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3){
			System.out.println("Please specify parameters: <amount of test data> <file> <format>");
			return;
		}
		
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()){
			System.out.println("File exists, please romove it manually: " + file);
			return;
		}
		
		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format)){
			saveContactsToCsvFile(contacts, file);
		} else if ("xml".equals(format)) {
			saveContactsToXmlFile(contacts, file);
		} else {
			System.out.println("Unknown format " + format);
		}
	}

	private static void saveContactsToXmlFile(List<ContactData> contacts, File file)  throws IOException{
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}
	
	public static List<ContactData> loadContactsFromXmlFile(File file) {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}
	private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getFirstname() + "," + contact.getLastname() + "," + contact.getAddress() + "," +
					"," + contact.getAddress2() + "," + contact.getHome_tel() + "," + contact.getMobile_tel() + "," +
					"," + contact.getWork_tel() + "," + contact.getEmail_1() + "," + contact.getEmail_2() + "," +
					"," + contact.getPhone2() + "," + contact.getByear() + "," + contact.getBmonth() + "," +
					"," + contact.getBday() + 
					",!" + "\n");
		} 
		writer.close();
	}
	
	  public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException{
			List<ContactData> list = new ArrayList<ContactData>();
			FileReader reader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader(reader);
			String line = bufferedReader.readLine();
			while (line != null) {
				String[] part = line.split(",");
				ContactData contact = new ContactData()
				.withFirstname(part[0])
				.withLastname(part[1])
				.withAddress(part[2])
				.withAddress2(part[3])
				.withHome_tel(part[4])
				.withMobile_tel(part[5])
				.withWork_tel(part[6])
				.withEmail_1(part[7])
				.withEmail_2(part[8])
				.withPhone2(part[9])
				.withByear(part[11])
				.withBmonth(part[12])
				.withBday(part[13]);
				list.add(contact);
				line = bufferedReader.readLine();
			}
			bufferedReader.close();
			return list;
		}

	public static List<ContactData> generateRandomContacts(int amount) {
		
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i < amount; i++) {
			String[] date = new String[3];
			date = generateRandomDate();
			ContactData contact = new ContactData()
			.withFirstname(generateRandomString())
			.withLastname(generateRandomString())
			.withAddress(generateRandomString())
			.withAddress2(generateRandomString())
			.withHome_tel(generateRandomString())
			.withMobile_tel(generateRandomString())
			.withWork_tel(generateRandomString())
			.withEmail_1(generateRandomString())
			.withEmail_2(generateRandomString())
			.withPhone2(generateRandomString())
			.withByear(date[0])
			.withBmonth(date[1])
			.withBday(date[2]);
			list.add(contact);
		}
		return list;
	}
	
	
	//-----------------------------------------------------------------------------------------------------
	//RandomGenerators
	//-----------------------------------------------------------------------------------------------------
		
		private static String generateRandomString(){
			Random rnd = new Random();
			if (rnd.nextInt(3) == 0) {
				return "";
			} else {
				return "test" + rnd.nextInt();
			}
		}
		
		private static String[] generateRandomDate(){
			String[] date = new String[3];
			ArrayList<String> month = new ArrayList<String>(); 
			month.add("January"); 
			month.add("February"); 
			month.add("March"); 
			month.add("April"); 
			month.add("May"); 
			month.add("June"); 
			month.add("July"); 
			month.add("August"); 
			month.add("September"); 
			month.add("October"); 
			month.add("November"); 
			month.add("December"); 
			
			GregorianCalendar gc = new GregorianCalendar();
			int year = randBetween(1900, 2014);
			gc.set(Calendar.YEAR, year);
			int dayOfYear = randBetween(1, gc.getActualMaximum(Calendar.DAY_OF_YEAR));
			gc.set(Calendar.DAY_OF_YEAR, dayOfYear);
			
			date[0] = ("" + gc.get(Calendar.YEAR));
			date[1] = ( month.get(gc.get(Calendar.MONTH) - 1));
			date[2] = ("" + gc.get(Calendar.DAY_OF_MONTH));
			return date;
			}
		
		private static int randBetween(int start, int end) {
	        return start + (int)Math.round(Math.random() * (end - start));
	    }
}